<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of login
 *
 * @author zwang
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class signup extends CI_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->model("signup_model");
    }

    public function index() {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if ($is_logged_in) {
            // $user_data = $this->session->userdata('validated');
            // $this->page_redirect($user_data['type']);
            $this->session->unset_userdata('validated');
            $this->session->unset_userdata('is_logged_in');
            $this->session->unset_userdata();
            $this->session->sess_destroy();
        }
        $this->load->view('signup');
    }

//    function validate_form() {
//        $firstName = $this->input->get_post('firstName');
//        $lastName = $this->input->get_post('lastName');
//        $email = $this->input->get_post('email');
//        $username = $this->input->get_post('username');
//        $password = $this->input->get_post('password');
//        $re_password = $this->input->get_post('re-password');
//        $result = true;
//        if ($this->signup_model->checkUsername($username)) {
//            $this->session->set_flashdata('username_fail', "Username already exists, please enter another username.");
//            $result = false;
//        }
//        if (strlen($password) < 7) {
//            $this->session->set_flashdata('password_fail', "Password is too short, minimum length 7");
//            $result = false;
//        }
//        if ($password != $re_password) {
//            $this->session->set_flashdata('rePassword_fail', "Password doesn't match the confirmation");
//            $result = false;
//        }
//        if ($this->signup_model->checkEmail($email)) {
//            $this->session->set_flashdata('email_fail', "Email already exists, please enter another email.");
//            $result = false;
//        }
//        if ($result) {
//            $userid = $this->signup_model->insertUser($username, $password);
//            if ($userid) {
//                if ($this->signup_model->insertUserProfile($userid, $firstName, $lastName, $email)) {
//                    $this->session->set_userdata('username', $username);
//                    $this->session->set_userdata('is_logged_in', TRUE);
//                    $this->session->set_userdata('validated', TRUE);
//                    redirect("user");
//                    return TRUE;
//                }
//            }
//        }
//        $this->session->set_flashdata('fail', "There were problems creating your account.");
//        redirect("signup");
//        return FALSE;
//    }

    function validate_form() {
        $this->form_validation->set_rules('firstName', 'firstName', 'trim|strip_tags|alpha_numeric|xss_clean|required');
        $this->form_validation->set_rules('lastName', 'lastName', 'trim|strip_tags|alpha_numeric|xss_clean|required');
        $this->form_validation->set_rules('username', 'username', 'trim|strip_tags|xss_clean|required|max_length[128]|is_unique[user.username]');
        $this->form_validation->set_rules('password', 'password', 'trim|required|matches[passconf]|min_length[7]|md5');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
        $this->form_validation->set_rules('email', 'email', 'trim|strip_tags|xss_clean|required|valid_email|is_unique[user_profile.email]');
//        $this->form_validation->set_rules('captcha', 'captcha', 'trim|strip_tags|xss_clean|callback_captcha_check|match_captcha[captcha.word]');
        $this->form_validation->set_error_delimiters('<span style="color: red; font-size: 11px">', '</span>');

        if ($this->form_validation->run() == FALSE) {
            $error = 'There were problems creating your account.';
            $this->load->view('signup', array('error'=>$error));
            return false;
        }else {
            $firstName = $this->input->post('firstName');
            $lastName = $this->input->post('lastName');
            $email = $this->input->post('email');
            $username = $this->input->post('username');
            $password = $this->input->post('password');
//            $passconf = $this->input->get_post('passconf');
            $userid = $this->signup_model->insertUser($username, $password);
            if ($userid) {
                if ($this->signup_model->insertUserProfile($userid, $firstName, $lastName, $email)) {
                    $this->session->set_userdata('username', $username);
                    $this->session->set_userdata('is_logged_in', TRUE);
                    $this->session->set_userdata('validated', TRUE);
                    $this->load->view('signup_success');
                    return TRUE;
                }
            }
        }
    }

}

?>
